/*
 * Copyright (c) 2014, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.test.mock;

import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

import org.omorphdb.core.index.IndexType;
import org.omorphdb.core.index.Indexes;
import org.omorphdb.core.storage.document.BaseField;
import org.omorphdb.core.storage.document.ChainedIndexField;
import org.omorphdb.core.storage.document.DocumentDescriptor;
import org.omorphdb.core.storage.document.Field;
import org.omorphdb.core.storage.document.FieldFactory;
import org.omorphdb.core.value.Value;
import org.omorphdb.core.value.ValueType;

/**
 * A mock of {@link FieldFactory}. It's just a basic implementation of such
 * interface, which is planed to be used in tests.
 * 
 * @author Marco Tamburelli
 */
public abstract class FieldFactoryMock implements FieldFactory
{
	private final List<String> registeredFields;
	private final List<String> deregisteredField;

	private final class IndexedField<T> implements Field<T>
	{
		private final String name;
		private Value<T> value;
		private Value<T> oldValue;

		private IndexedField(String name)
		{
			this.name = name;
		}

		@Override
		public String name()
		{
			return name;
		}

		@Override
		public Value<T> getValue()
		{
			return this.value;
		}

		@Override
		public void setValue(Value<T> value, boolean updateAware)
		{
			if (updateAware) this.value = value;
			else this.oldValue = this.value = value;
		}

		@Override
		public void doAfterSave() throws IOException
		{
			if (this.oldValue != null) FieldFactoryMock.this.deregisteredField.add(this.name);

			FieldFactoryMock.this.registeredFields.add(this.name);
		}
	}

	public FieldFactoryMock()
	{
		this.registeredFields = new ArrayList<>();
		this.deregisteredField = new ArrayList<>();
	}

	/**
	 * The returned mock has 2 additional methods:
	 * {@link #getRegisteredFields()} and {@link #getDeregisteredField()}, which
	 * takes notes of fields to be registered/deregistered in the proper
	 * indexes.
	 * 
	 * @return
	 */
	public static FieldFactoryMock createWithIndexRegistration()
	{
		return new FieldFactoryMock()
		{
			@Override
			public <T> Field<T> createField(DocumentDescriptor docDescr, String name, ValueType type)
			{
				return new IndexedField<T>(name);
			}
		};
	}

	/**
	 * Provides fields with no index support.
	 * 
	 * @return
	 */
	public static FieldFactoryMock createWithBaseFields()
	{
		return new FieldFactoryMock()
		{
			@Override
			public <T> Field<T> createField(DocumentDescriptor docDescr, String name, ValueType type)
			{
				return new BaseField<>(name);
			}
		};
	}

	/**
	 * Provides fields indexed with an hash index.
	 * 
	 * @return
	 */
	public static FieldFactoryMock createWithHashIndex(final Indexes indexes, ValueType... types)
	{
		final EnumSet<ValueType> set = EnumSet.of(types[0], types);

		return new FieldFactoryMock()
		{
			@Override
			public <T> Field<T> createField(DocumentDescriptor docDescr, String name, ValueType type)
					throws IOException
			{
				if (set.contains(type))
				{
					IndexType[] indexTypes = new IndexType[] { IndexType.HASH };
					return new ChainedIndexField<>(docDescr, name, type, indexTypes, indexes);
				}
				else
				{
					return new BaseField<>(name);
				}
			}
		};
	}

	/**
	 * Returns the number of expected registered fields.
	 * 
	 * @return
	 */
	public List<String> getRegisteredFields()
	{
		return this.registeredFields;
	}

	/**
	 * Returns the number of expected deregistered fields.
	 * 
	 * @return
	 */
	public List<String> getDeregisteredField()
	{
		return this.deregisteredField;
	}
}
